<?php
//------- logistic wxapp -------
namespace api\logi\service;

use api\logi\model\LogiPostModel;
use think\Db;
class PostService {

    public function postList($filter, $more = false) {
        $where = [
            'a.create_time' => ['>=', 0],
            'a.post_status' => ['>', 0],
            'a.delete_time' => 0,
            'a.user_id'     => $filter['user_id'],
        ];

        $join = [
            ['__USER__ u', 'a.user_id = u.id'],
            ['__LOGI_STATUS__ b', 'a.post_status = b.id','left'],
        ];

        $field = 'a.*,u.user_login,u.mobile,u.user_nickname,u.user_email,b.name as s_name';

        $logiPostModel = new LogiPostModel();
        $res = $logiPostModel->alias('a')->field($field)
            ->join($join)
            ->where($where)
            ->order('id', 'DESC')
            ->paginate(10);

        return $res;

    }

    public function publishedLogi($filter) {
        $logiPostModel = new LogiPostModel();

        $where = [
            'a.delete_time'    => 0,
            'a.id'             => $filter['id'],
            'a.user_id'        => $filter['user_id'],
        ];
        $join = [
            ['__ADDRESS__ a1', 'a.stage = a1.id','left'],
            ['__ADDRESS__ a2', 'a.station = a2.id','left'],
            ['__ADDRESS__ a3', 'a.addr = a3.id','left'],
            ['__LOGI_EXPRESS__ e1', 'a.id = e1.logi_id and e1.status=2','left'],
            ['__LOGI_EXPRESS__ e2', 'a.id = e2.logi_id and e2.status=3','left'],
        ];

        $field = 'a.*,
            a1.name as sg_name,a1.zone as sg_zone,a1.contact as sg_contact,a1.receive as sg_receive,
            a2.name as st_name,a2.zone as st_zone,a2.contact as st_contact,a2.receive as st_receive,
            a3.name as a_name,a3.zone as a_zone,a3.contact as a_contact,a3.receive as a_receive,
            e1.express_num as stage_logi,
            e2.express_num as station_logi';

        $res = $logiPostModel->alias('a')->join($join)->field($field)->where($where)->find();
        $res->contact = has_plus($res->contact);
        $res->sg_zone = has_plus($res->sg_zone);
        $res->st_zone = has_plus($res->st_zone);
        $res->a_zone = has_plus($res->a_zone);
        // $res->stage_logi = Db::name('logiExpress')->where(['logi_id'=>$res->id,'status'=>2])->value('express_num');
        // $res->station_logi = Db::name('logiExpress')->where(['logi_id'=>$res->id,'status'=>3])->value('express_num');
        return $res;
    }

}